'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const {
    formatDate
} = require('../../utils/index.js');
module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, DECIMAL, TEXT } = app.Sequelize;
    const TravelSkuStock = app.model.define(
        'TravelSkuStock',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            travel_sku_id: {type: INTEGER, allowNull: false, comment: '旅游SKU ID'},
            travel_date: {
                type: DATE,
                allowNull: false,
                comment: '旅游日期',
                get() {
                    return formatDate(this.getDataValue('travel_date'), 'YYYY-MM-DD');
                }
            },
            adult_total_stock: {type: INTEGER, defaultValue: 0, comment: '成人总库存数量'},
            adult_available_stock: {type: INTEGER, defaultValue: 0, comment: '成人可用库存数量'},
            adult_reserved_stock: {type: INTEGER, defaultValue: 0, comment: '成人已预订库存数量'},
            adult_sold_stock: {type: INTEGER, defaultValue: 0, comment: '成人已售出库存数量'},
            child_total_stock: {type: INTEGER, defaultValue: 0, comment: '儿童总库存数量'},
            child_available_stock: {type: INTEGER, defaultValue: 0, comment: '儿童可用库存数量'},
            child_reserved_stock: {type: INTEGER, defaultValue: 0, comment: '儿童已预订库存数量'},
            child_sold_stock: {type: INTEGER, defaultValue: 0, comment: '儿童已售出库存数量'},
            adult_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '当日成人价格'
            },
            child_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '当日儿童价格'
            },
            adult_original_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '当日成人原价'
            },
            child_original_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '当日儿童原价'
            },
            single_price_diff: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '单房价差'
            },  
            child_price_description: {
                type: STRING(500),
                comment: '儿童价格描述'
            },
            is_group: {
                type: TINYINT,
                defaultValue: 0,
                comment: '是否拼团：1-是，0-否'
            },
            // 拼团人数
            group_adult_people: {
                type: INTEGER,
                defaultValue: 0,
                comment: '拼团人数'
            },
            group_child_people: {
                type: INTEGER,
                defaultValue: 0,
                comment: '拼团儿童人数'
            },
            group_adult_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '拼团成人价格'
            },
            group_child_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '拼团儿童价格'
            },
            hotel_levels: {type: TEXT, comment: '酒店级别配置，JSON格式存储，包含级别名称、价格、描述、单房差等信息'},
            status: {type: TINYINT, defaultValue: 1, comment: '状态：1-可预订，0-不可预订'},
            remark: {type: STRING(500), comment: '备注'},
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'travel_sku_stock',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time',
            indexes: [
                {
                    unique: true,
                    fields: ['travel_sku_id', 'travel_date']
                },
                {
                    fields: ['travel_date']
                }
            ]
        }
    );
    
    TravelSkuStock.associate = function() {
        TravelSkuStock.belongsTo(app.model.Business.TravelSku, { foreignKey: 'travel_sku_id', sourceKey: 'id', as: 'travelSku' });
    }

    return TravelSkuStock;
}; 